package edu.berkeley.guir.webquilt.proxy;

import com.arthurdo.parser.*;
import edu.berkeley.guir.webquilt.*;

/**
 * Inserts debugging links at the top of the page right after the
 *  "body" tag. Includes links for viewing the current logfile,
 *  submitting bug reports, returning to the start page, and
 *  resetting the user session.
 *
 * @author  Jeffrey Heer (
 *          <A HREF="mailto:jheer@hkn.eecs.berkeley.edu">jheer@hkn.eecs.berkeley.edu</A>
 *          )
 * @since   JDK 1.3
 * @version WebQuilt-v1.0-1.0.0, Feb 21 2001
 */
class HTMLDebugBodyEditor extends HTMLAbstractEditor implements TagEditor {

    private boolean sync     = false;
    private boolean bodyFlag = false;

    public void setSync(boolean sync) {
	this.sync = sync;
    } //

    public String editTag(HtmlTag tag) {

	if ( bodyFlag ) {
	    return tag.toString() + "\n";
	}

	if ( sync ) {
	    String strOnload = tag.getParam("onload");
	    if ( strOnload == null ) { strOnload = ""; }
	    strOnload = "window.open(\'" + proxy.strSvrBase + proxy.PROXY_SYNC +
		        "?tid=" + entry.nextTID + "\','_wqSync'); " + strOnload;
	    tag.setParam("onload", strOnload);
	}

	String strTag   = tag.toString() + "\n";


	String strHome  = "<a href=\"" + proxy.strSvrUrl + "\" " +
	                  "onMouseOver=\"self.status='Return to the WebQuilt Start Page';\">\n" +
	                  "Home\n</a>\n";

	String strLog   = "<a href=\"" +
	                  "javascript:window.open(\'" +
	                  proxy.strSvrBase + proxy.PROXY_LOGVIEW +
	                  "\',\'_wqLog\',\'resizable=yes,toolbar=1," +
	                  "location=0,directories=0,status=0,menubar=0,scrollbars=1," +
	                  "copyhistory=0,width=600,height=350\');\" " +
	                  "target=\"_wqLog\" " +
	                  "onMouseOver=\"self.status='View your WebQuilt log';\">\n" +
	                  "Log File\n</a>\n";

	String strBug   = "<a href=\"" +
                          "javascript:window.open(\'" +
                          proxy.strSvrBase + proxy.PROXY_BUGREP + "?url=" + entry.strOrig +
                          "\',\'_wqBug\',\'resizable=yes,toolbar=1," +
	                  "location=0,directories=0,status=0,menubar=0,scrollbars=1," +
	                  "copyhistory=0,width=450,height=350\');\" " +
	                  "target=\"_wqBug\" " +
	                  "onMouseOver=\"self.status='Submit a Bug Report';\">\n" +
	                  "Submit Bug Report\n</a>\n";

	String strView  = "<a href=\"" +
	                  "javascript:window.open(\'"
	                  + proxy.strSvrBase + proxy.PROXY_SYNC +
		          "?tid=" + entry.nextTID +
		          "\','_wqSync');\" target=\"_wqSync\" " +
	                  "onMouseOver=\"self.status='View this page normally (non-proxied)';\">\n" +
	                  "View Page\n</a>\n";

	String strSync;

	if ( sync ) {
	       strSync  = "<a href=\"" +
	                  "javascript:window.open(\'" +
	                  proxy.strSvrBase + proxy.PROXY_SYNC + "?sync=false" +
	                  "\',\'_wqSync\');\" " + "target=\"_wqSync\" " +
	                  "onMouseOver=\"self.status='Disable page synchronization';\">\n" +
	                  "Sync Off\n</a>\n";
	} else {
	       strSync  = "<a href=\"" +
	                  "javascript:window.open(\'" +
	                  proxy.strSvrBase + proxy.PROXY_SYNC + "?tid=" + entry.nextTID + "&sync=true" +
	                  "\',\'_wqSync\');\" " + "target=\"_wqSync\" " +
	                  "onMouseOver=\"self.status='Synchronize unproxied page view with proxied surfing';\">\n" +
	                  "Sync On\n</a>\n";
	}

	String strReset = "<a href=\"" + proxy.strSvrBase + proxy.PROXY_RESET + "\" " +
	                  "onMouseOver=\"self.status='Invalidate the current session';\">\n" +
	                   "Reset\n</a>\n";

	bodyFlag = true;

	return ( strTag   + "WebQuilt Debug Mode -- " + strHome + " - " + strLog   + " - " +
                 strBug + " - " + strView + " - " + strSync + " - " + strReset + "\n<BR><BR>\n" );
    } //


    public void reset() {
	bodyFlag = false;
    } //


} // of class
//==============================================================================

/*
Copyright (c) 2001 Regents of the University of California.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

3. All advertising materials mentioning features or use of this software
   must display the following acknowledgement:

      This product includes software developed by the Group for User
      Interface Research at the University of California at Berkeley.

4. The name of the University may not be used to endorse or promote products
   derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/

